<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd" style="margin-right: 5px">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <EsIndexModal @register="registerModal" @success="reload" />
    <DetailModal @register="register" :esIndexVlaue="esIndexVlaue" />
  </div>
</template>
<script lang="ts" name="search-esindex" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import DetailModal from './DetailModal.vue';
  import { getIndicesList, deleteIndex, getIndex } from './esindex.api';
  import { columns, searchFormSchema } from './esindex.data';
  import EsIndexModal from './EsIndexModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';

  const esIndexVlaue = ref('');

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [register, { openModal: openDetail }] = useModal();

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: 'search-esindex',
    tableProps: {
      title: 'ES索引列表',
      api: getIndicesList,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
        fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
      },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '查看',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          placement: 'left',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 查看
   */
  async function handleDetail(record) {
    //获取详情
    const data = await getIndex({ indexName: record.index });
    esIndexVlaue.value = JSON.stringify(data);
    openDetail(true);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteIndex({ indexName: record.index }, reload);
  }
</script>
